  
import arcpy
import os

def get_script_path():
    # Get the absolute path of the currently running script
    return os.path.abspath(__file__)

wkdir = os.path.dirname(get_script_path())


# fishnet
fishnet = r'%s\local\gis_data\003_boundaries\fishnet\fishnet_lake_chad_ntl_adm0_x_d01.shp' % wkdir
fishnet_copy = "in_memory\\fishnet_cp"

# https://icr.ethz.ch/data/greg/
greg = r'%s\local\gis_data\016_society\GREG\GREG.shp'
# Murdock, George Peter, “Africa its peoples and their culture history,” 1959.
# https://github.com/sboysel/murdock/blob/master/data-raw/Murdock_EA_2011_vkZ.geojson
murd = r'%s\local\gis_data\Ethnicity_murdock\Murdock_EA_2011_vkZ.shp'

# ethnic  
murd_copy = "in_memory\\murd_copy"

# output
fishnetIntersectgreg = r'%s\proc_data\greg_intersect.shp' % wkdir
fishnetIntersectgregpt = r'%s\proc_data\greg_intersect_join_pt.shp' % wkdir

# output
fishnetIntersectmurd = r'%s\proc_data\murd_intersect.shp' % wkdir
fishnetIntersectmurdpt = r'%s\proc_data\murd_intersect_join_pt.shp' % wkdir

## copy fishnet and only keep objectid and gid_0 fields
arcpy.CopyFeatures_management(fishnet,fishnet_copy)
exclude = ["OBJECTID", "GID_0"]
fieldNameList = []
for field in arcpy.ListFields(fishnet_copy):  
     if not field.required:  
          if not field.name in exclude:  
               fieldNameList.append(field.name)  
arcpy.DeleteField_management(fishnet_copy,fieldNameList)

##
## greg intersections
##
     
if arcpy.Exists(fishnetIntersectgreg):
    arcpy.Delete_management(fishnetIntersectgreg)
if not arcpy.Exists(fishnetIntersectgreg):   
    fishnet_lyr = 'fishnet_lyr'
    arcpy.MakeFeatureLayer_management(fishnet_copy,fishnet_lyr)
    #arcpy.SelectLayerByLocation_management(fishnet_lyr,"INTERSECT",greg)
    print(arcpy.GetCount_management(fishnet_lyr))
    arcpy.Identity_analysis(fishnet_lyr, greg, fishnetIntersectgreg,"ONLY_FID")

## calculate ethnic area ##
gregArea = "greg_km2"
fieldnames = [f.name for f in arcpy.ListFields(fishnetIntersectgreg)]
if gregArea not in fieldnames:
    arcpy.AddField_management(fishnetIntersectgreg, gregArea, "DOUBLE", 18, 6)
arcpy.CalculateField_management(fishnetIntersectgreg, gregArea,"!shape.area@kilometers! / 1000000")

print('coastline misalignment causes missings...')

# feature to point #
#arcpy.Delete_management("in_memory\\greg_join_pt")
arcpy.FeatureToPoint_management(fishnetIntersectgreg,"in_memory\\greg_join_pt","INSIDE")

# out_feature_class, {join_operation}, {join_type}, {field_mapping},
if arcpy.Exists(fishnetIntersectgregpt):
    arcpy.Delete_management(fishnetIntersectgregpt)
# merge nearest poly in GREG original for boundary mismatch #
arcpy.SpatialJoin_analysis("in_memory\\greg_join_pt",greg,fishnetIntersectgregpt,"","","","CLOSEST")

print("done")



##
## ethnic intersections : Murdock 2011
##

arcpy.CopyFeatures_management(murd,murd_copy)
murd_dfields = ["V" + str(x) for x in list(range(1,114))]
arcpy.DeleteField_management(murd_copy,murd_dfields)


murd_lyr = "murd_lyr"
arcpy.MakeFeatureLayer_management(murd,murd_lyr)     
if arcpy.Exists(fishnetIntersectmurd):
    arcpy.Delete_management(fishnetIntersectmurd)
if not arcpy.Exists(fishnetIntersectmurd):
    fishnet_lyr = 'fishnet_lyr'
    #arcpy.MakeFeatureLayer_management(fishnet,fishnet_lyr)
    #arcpy.SelectLayerByLocation_management(fishnet_lyr,"INTERSECT",murd_lyr)
    print(arcpy.GetCount_management(fishnet_lyr))
    arcpy.Identity_analysis(fishnet_lyr, murd_copy, fishnetIntersectmurd,"ONLY_FID")
    
## calculate ethnic area ##
murdArea = "murd_km2"
fieldnames = [f.name for f in arcpy.ListFields(fishnetIntersectmurd)]
if murdArea not in fieldnames:
    arcpy.AddField_management(fishnetIntersectmurd, murdArea, "DOUBLE", 18, 6)
arcpy.CalculateField_management(fishnetIntersectmurd, murdArea,"!shape.area@kilometers! / 1000000")

print('coastline misalignment causes missings...')

# feature to point #
arcpy.FeatureToPoint_management(fishnetIntersectmurd,"in_memory\\murd_join_pt","INSIDE")

if arcpy.Exists(fishnetIntersectmurdpt):
    arcpy.Delete_management(fishnetIntersectmurdpt)
    
# out_feature_class, {join_operation}, {join_type}, {field_mapping},
arcpy.SpatialJoin_analysis("in_memory\\murd_join_pt",murd_copy,fishnetIntersectmurdpt,"","","","CLOSEST")

murd_dfields = ["V" + str(x) for x in list(range(1,114))]
arcpy.DeleteField_management(fishnetIntersectmurdpt,murd_dfields)

print("done")
